<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable">
     <!--插槽:table标题-->
      <template #tableTitle>
        <a-space>
          <a-input style="border-radius: 5px;"  placeholder="分组"/>
          <a-input style="border-radius: 5px;"  placeholder="用户ID"/>
          <a-input style="border-radius: 5px;"  placeholder="用户名"/>
          <a-button type="primary" shape="round" @click="handleSyncData">搜索</a-button>
          <a-button type="danger" shape="round" @click="handleSyncData">同步</a-button>
        </a-space>
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"/>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <VmUserSettingsModal @register="registerModal" @success="handleSuccess"></VmUserSettingsModal>
  </div>
</template>

<script lang="ts" name="vm-vmUserSettings" setup>
  import {BasicTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import VmUserSettingsModal from './components/VmUserSettingsModal.vue'
  import {columns, searchFormSchema} from './VmUserSettings.data';
  import {list, getImportUrl,getExportUrl,syncData} from './VmUserSettings.api';

  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册table数据
  const {tableContext } = useListPage({
      tableProps:{
           title: '用户基础配额设置',
           api: list,
           columns,
           canResize:false,
           useSearchForm:false,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 150,
               fixed:'right'
            },
      },
       exportConfig: {
            name:"用户基础配置",
            url: getExportUrl,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

  const [registerTable, {reload},{  selectedRowKeys }] = tableContext

   /**
    * 同步分组数据
    */
  function handleSyncData() {
    console.log("正在同步用户数据")
    syncData().then((data) => {
      console.log(data)
      reload();
    }).catch((err) => {
      console.log(err)
    })
  }

   /**
    * 编辑事件,编辑可以保留
    */
  function handleEdit(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
   }
   /**
    * 详情
   */
  function handleDetail(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }

   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
       return [
         {
           label: '编辑',
           onClick: handleEdit.bind(null, record),
         },
         {
          label: '详情',
           onClick: handleDetail.bind(null, record),
         }
       ]
   }
</script>

<style scoped>

</style>